﻿using System;
using System.Collections.ObjectModel;

namespace LayeredArchitectureSample.DomainModel
{
    [GenerateDataTransferObject(TemplateName = "DefaultDtoTemplate.tt")]
    public class Trip : DomainObject, ITrip
    {
        private ITrip _tripCurrentState;

        public Trip(ITrip tripCurrentState): base(tripCurrentState.Id)
        {
            if (tripCurrentState == null) throw new ArgumentNullException("tripCurrentState");

            _tripCurrentState = tripCurrentState;
        }

        public ReadOnlyCollection<WareUnitsCounted> Wares
        {
            get { return _tripCurrentState.Wares; }
        }

        public Route Route
        {
            get { return _tripCurrentState.Route; }
        }

        /// <exception cref="InvalidOperationException"></exception>
        public void MarkAsArrivedToFinalDestination()
        {
            // TODO: replace with polymorphism
            if (_tripCurrentState.GetType() == typeof(TripEnRoute))
            {
                _tripCurrentState = new TripThatHasArrived(base.Id, _tripCurrentState.Wares, _tripCurrentState.Route);
            }
            else
            {
                _tripCurrentState.MarkAsArrivedToFinalDestination();
            }
        }

        public string Status
        {
            get { return _tripCurrentState.Status; }
        }

        public TripState State
        {
            get { return _tripCurrentState.State; }
        }
    }
}